/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mvc.example.presentation.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import mvc.example.presentation.listeners.StudentDataListener;
import mvc.example.bussines.domain.Student;

/**
 *
 * @author zeta
 */
public class StudentsModel extends AbstractModel{
   
    private List<Student> students;
    private Student selectedStudent;

    public StudentsModel() {
        students = new ArrayList<Student>();
    }


    public StudentsModel(List<Student> students) {
        this.students = new ArrayList<Student>(students.size());
       
        for (Student student : students) {
            this.students.add(student);
        }

        if ( this.students.size() > 0 ) {
            this.selectedStudent = students.get( 0 );
        }
    }

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }

    public Student getSelectedStudent() {
        return selectedStudent;
    }

    public void setSelectedStudent(Student student) {
         if ((selectedStudent != null && !selectedStudent.equals( student )) ||
	    selectedStudent == null && student != null) {
	    selectedStudent = student;
	    fireContentsChanged(this, -1, -1);
        }
    }


    public void addDataListener(StudentDataListener dataListener) {
        listenerList.add(StudentDataListener.class, dataListener);
    }
    
    public void addStudent(Student student){
        getStudents().add(student);
        fireIntervalAdded(this,getStudents().size()-1);
        setSelectedStudent( student );
    }

    public void remplaceStudent(Student student) {
        int indexOf = getStudents().indexOf(student);
        getStudents().set(indexOf, student);
        setSelectedStudent( student );
        fireContentsChanged(this, -1, -1);
    }

    
   
         
    
}
